//
//  MainTabBarViewController.swift
//  swiftWeiBoDemo
//
//  Created by zzw on 16/3/14.
//  Copyright © 2016年 zzw. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        

        addChildViewControllers()
  
        
    }
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
      addBtn()
    }
    
        private func addBtn(){
        
        tabBar.addSubview(createBtn)
        
            let width = UIScreen.mainScreen().bounds.size.width/5
            
            let rect = CGRect(x: 0, y: 0, width: width, height: 49)

            createBtn.frame = CGRectOffset(rect, 2 * width, 0)
    
    }
    //MARK -添加所有子控制器-
    private  func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
       // tabBar.barTintColor = UIColor.redColor()
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        if let jsonPath = path{
             
            let jsonData = NSData(contentsOfFile: jsonPath)
            
            
            do{
                
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                for dict in dictArr as! [[String:String]]{
                    
                    addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                }
            }catch{
                
                addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
                addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                addChildViewController("NullViewController", title: "", imageName: "")
                addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
                addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
            }
        }
    }

    /**
     初始化子控制器
     
     - parameter childController: 需要初始化的子控制器
     - parameter title:           子控制器题目
     - parameter imageName:       子控制器图片
     */
    private func addChildViewController(childControllerName: String,title:String,imageName:String) {
        
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let cls:AnyClass? = NSClassFromString(ns + "." + childControllerName)
        let clsVC = cls as! UIViewController.Type
        
        let vc = clsVC.init()
        
        
    
        vc.tabBarItem.image = UIImage(named: imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        
        vc.title = title
        
        let nav = UINavigationController()
        
        nav.addChildViewController(vc)
        
        addChildViewController(nav)
        
    }
    //MARK -懒加载-
    private lazy var createBtn:UIButton = {
    
    let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    
    }()
    
    func btnClick(){
    
       print(__FUNCTION__)
        
    
    }
}
